const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const openid = wxContext.OPENID
  const { roomId, roomStatus, senderName, content } = event.data
  return db.collection('message').add({
    data: {
      roomId,
      roomStatus,
      sender: openid,
      senderName: senderName,
      content,
      time: new Date()
    }
  })
}